using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemySight : MonoBehaviour
{
    [Header("Vision Settings")]
    public LayerMask targetLayer = 1 << 3;
    public float viewDistance = 2.5f;
    public float viewAngle = 20f;

    [Header("Debug")]
    public bool showVisionArea = true;
    public bool showDirection = true;

    public Transform playerTransform;
    public EnemyStateMachine stateMachine;
    public EnemyCombat enemyCombat;
    [HideInInspector] public EnemyMovement enemyMovement;

    void Awake()
    {
        stateMachine = GetComponent<EnemyStateMachine>();
        enemyMovement = GetComponent<EnemyMovement>();
        playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
    }

    public void CheckForPlayerInSight()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, viewDistance, targetLayer);

        foreach (Collider2D col in colliders)
        {
            if (col.CompareTag("Player"))
            {
                Vector2 dirToTarget = ((Vector2)col.transform.position - (Vector2)transform.position).normalized;
                float angle = Vector2.Angle(transform.up, dirToTarget);

                if (angle <= viewAngle)
                {
                    float distance = Vector2.Distance(transform.position, col.transform.position);
                    RaycastHit2D hit = Physics2D.Raycast(transform.position, dirToTarget, distance, LayerMask.GetMask("Obstacle", "Wall"));
                    if (!hit.collider)
                    {
                        stateMachine.OnPlayerDetected(col.transform.position);
                    }
                }
            }
            else if (col.CompareTag("Body"))
            {
                Vector2 dirToTarget = ((Vector2)col.transform.position - (Vector2)transform.position).normalized;
                float angle = Vector2.Angle(transform.up, dirToTarget);

                if (angle <= viewAngle)
                {
                    float distance = Vector2.Distance(transform.position, col.transform.position);
                    if (distance <= viewDistance)
                    {
                        if (enemyMovement.patrolCenter != (Vector2)col.transform.position)
                        {
                            enemyMovement.patrolCenter = col.transform.position;
                            stateMachine.OnBodyDetected(col.transform.position);
                            enemyCombat.AskOther(transform.position, 100);
                        }
                    }
                }
            }
        }
    }

    private void OnDrawGizmos()
    {
        if (!showVisionArea) return;

        Gizmos.color = new Color(1, 1, 0, 0.1f);
        Gizmos.DrawSphere(transform.position, viewDistance);

        Vector3 rightBound = Quaternion.Euler(0, 0, viewAngle) * transform.up * viewDistance;
        Vector3 leftBound = Quaternion.Euler(0, 0, -viewAngle) * transform.up * viewDistance;

        Gizmos.color = Color.red;
        Gizmos.DrawRay(transform.position, rightBound);
        Gizmos.DrawRay(transform.position, leftBound);

        DrawArc(transform.position, viewDistance, -viewAngle, viewAngle, Color.red);
    }

    private void DrawArc(Vector3 center, float radius, float fromAngle, float toAngle, Color color)
    {
        int segments = 20;
        float angleStep = (toAngle - fromAngle) / segments;
        Vector3 startDir = Quaternion.Euler(0, 0, fromAngle) * Vector3.up;
        Vector3 prevPoint = center + startDir * radius;

        for (int i = 1; i <= segments; i++)
        {
            float angle = fromAngle + i * angleStep;
            Vector3 dir = Quaternion.Euler(0, 0, angle) * Vector3.up;
            Vector3 nextPoint = center + dir * radius;
            Debug.DrawLine(prevPoint, nextPoint, color);
            prevPoint = nextPoint;
        }
    }
}